module.exports = function ($resource, $state, $http, $q, $scope, FileUploader, $modal, toaster, $stateParams, date2str) {
    /* 分页
  * ========================================= */
  $scope.maxSize = 5;             //最多显示多少个按钮
  $scope.bigCurrentPage = 1;      //当前页码
  $scope.itemsPerPage = 10         //每页显示几条
  $scope.obj={};
  $scope.d={};
  $scope.searchform = {};
  $scope.vm = {
      'date' : '',
      'options' : {
          format: "YYYY-MM-DD",
          locale : 'zh-cn'
      }
  }
  $scope.vm1 = {
      'date' : '',
      'options' : {
          format: "YYYY-MM-DD",
          locale : 'zh-cn'
      }
  }
    // 公告类型
  $scope.typeFileArr = [
      {name:'旅游龙头项目',value:'0', id : '1'},
      {name:'旅游集聚区项目',value:'1', id : '2'},
      {name:'旅游集群项目',value:'2', id : '3'},
      {name:'全域旅游示范区项目',value:'3', id : '4'},
      {name:'特色旅游休闲街区项目',value:'4', id : '5'},
      {name:'特色旅游产业园区项目',value:'5', id : '6'},
      {name:'特色旅游人才培训基地项目',value:'6', id : '7'},
      {name:'特色旅游创客孵化基地项目',value:'7', id : '8'},
      {name:'离境退税项目',value:'8', id : '9'},
      {name:'景区提升工程项目',value:'9', id : '1'},
      {name:'旅游度假区项目',value:'10', id : '2'},
      {name:'乡村旅游项目',value:'11', id : '3'},
      {name:'旅游公共服务体系项目',value:'12', id : '4'},
      {name:'旅游公共交通体系项目',value:'13', id : '5'},
      {name:'旅游智慧化项目',value:'14', id : '6'},
      {name:'旅游节庆活动项目',value:'15', id : '7'},
      {name:'旅游展示活动项目',value:'16', id : '8'},
      {name:'旅游惠民活动项目',value:'17', id : '9'},
      {name:'旅游体制改革项目',value:'18', id : '8'},
      {name:'旅游招徕补贴项目',value:'19', id : '9'},
      {name:'旅游品牌奖励项目',value:'20', id : '1'},
      {name:'宣传营销项目',value:'21', id : '2'},
      {name:'旅游产品研发项目',value:'22', id : '3'},
      {name:'课题调查研究项目',value:'23', id : '4'},
      {name:'区域旅游合作项目',value:'24', id : '5'},
      {name:'旅游招商项目',value:'25', id : '6'},
      {name:'行业管理项目',value:'26', id : '7'},
      {name:'满意度评价项目',value:'27', id : '8'},
      {name:'其他项目',value:'28', id : '9'}     
  ]
  // 所属区县
  $scope.areaarr = [
    {'name' : '和平区', 'value' : 'L0002'},
    {'name' : '沈河区', 'value' : 'L0002'},
    {'name' : '皇姑区', 'value' : 'L0002'},
    {'name' : '铁西区', 'value' : 'L0002'},
    {'name' : '大东区', 'value' : 'L0002'},
    {'name' : '东陵区', 'value' : 'L0002'},
    {'name' : '于洪区', 'value' : 'L0002'},
    {'name' : '苏家屯区', 'value' : 'L0002'},
    {'name' : '沈北新区', 'value' : 'L0002'},
    {'name' : '浑南新区', 'value' : 'L0002'},
    {'name' : '辽中区', 'value' : 'L0002'},
    {'name' : '康平县', 'value' : 'L0002'},
    {'name' : '法库县', 'value' : 'L0002'},
    {'name' : '新民市', 'value' : 'L0002'}
 ]
 $scope.type = $stateParams.type;
  console.log($state.params.type);
  if ($stateParams.type == '0'){
      $resource('/api/ac/ggc/reviewCompanyService/getApplyCompanyInfo', {}, {}).save({}, function(res){
          if(res.errcode != 0){ 
              toaster.error({title:"",body:res.errmsg});
              return;
          }
          console.log('查询企业信息是否完整');
          console.log(res);
          if (res.data.msg == '0') {
            if (confirm('请先完善企业信息')) {
              $state.go('app.completeinfos');
            } else {
              $state.go('app.completeinfos');
            }
          }
      });
  }
  $scope.info = {
      'start_time' : '',
      'end_time' : ''
  }
  $scope.levelArr = [
    {'name' : '国家级', 'value' : '3'},
    {'name' : '省级', 'value' : '2'},
    {'name' : '区域级', 'value' : '4'},
    {'name' : '市级', 'value' : '1'}
  ]
  $scope.getlevel = function(project_level){
    for (var i = 0; i < $scope.levelArr.length; i++){
      if(project_level == $scope.levelArr[i].value){
        project_level = $scope.levelArr[i].name;
        return project_level;
      }
    }
  }
  $scope.pageChanged = function () {
      if ($scope.vm.date._d == undefined) {
          $scope.info.start_time = '';
      } else {
        $scope.info.start_time = date2str($scope.vm.date._d);
      }
      if ($scope.vm1.date._d == undefined) {
          $scope.info.end_time = '';
      } else {
          $scope.info.end_time = date2str($scope.vm1.date._d);
      }
      console.log(($scope.vm.date._d));
      var para = {
          pageNo:$scope.bigCurrentPage,
          pageSize:$scope.itemsPerPage
      };
      para = angular.extend($scope.searchform, para, $scope.info);
      if($state.params.type == 0) {
        $resource('/api/ac/ggc/reviewCompanyProjectService/findInfoList', {}, {}).save(para, function(res){	
            if(res.errcode === 0 || res.errcode === 10003){
                $scope.a = res.data.results;
                $scope.b = angular.copy(res.data.results);
                $scope.b.forEach(element => {
                  element.project_build = element.project_build=='0'?'建筑类':'非建筑类';
                  element.city_project = element.city_project=='0'?'需要':'不需要';
                  element.project_level = $scope.getlevel(element.project_level);
                });
                console.log('下面是新数据') 
                console.log($scope.b);
                $scope.bigTotalItems = res.data.totalRecord;
            }else{
                alert(res.errmsg);
            }
        });
    } else {
        console.log('这是1')
        $resource('/api/ac/ggc/reviewCompanyProjectService/findInfoByAdminList', {}, {}).save(para, function(res){	
            if(res.errcode === 0 || res.errcode === 10003){
                $scope.a=res.data.results;
                $scope.b = angular.copy(res.data.results);
                $scope.b.forEach(element => {
                  element.project_build = element.project_build=='0'?'建筑类':'非建筑类';
                  element.city_project = element.city_project=='0'?'需要':'不需要';
                  element.project_level = $scope.getlevel(element.project_level);
                });
                console.log('下面是新数据') 
                console.log($scope.b);
                $scope.bigTotalItems = res.data.totalRecord;					
                console.log($scope.a);
                console.log('上面是企业项目列表返回值') 
            }else{
                alert(res.errmsg);
            }
        });
    }
      
  };
  $scope.pageChanged();

  $scope.search=function(){
      var dic = {
          pageNo:$scope.currentPage,
          pageSize:$scope.itemsPerPage,
      }
      $resource('', {}, {}).save(dic, function(res){
          if(res.errcode === 0 || res.errcode === 10003){
              $scope.a=res.data.results;
              console.log($scope.a);
              $scope.totalItems = res.data.totalRecord;
          }else{
              alert(res.errmsg);
          }
      });
  }

  $scope.toinfo= function (info) {
    if ($stateParams.type == '1'){
      $scope.type = 'gfonlyinfo';	
      var modalInstance = $modal.open({
        template: require('../views/project_search_info.html'),
        controller: 'project_search_infos',
        size: 'lg',
        resolve: {
          iteminfo: function () {
            return info;
          },
          'type': function () {
            return 'gfonlyinfo';
          }
        }
      });
      modalInstance.result.then(function (showResult) {	
          $scope.pageChanged();
      });
    } else if ($stateParams.type == '0'){
      $scope.type = 'qyonlyinfo';	
      var modalInstance = $modal.open({
        template: require('../views/project_search_info.html'),
        controller: 'project_search_infos',
        size: 'lg',
        resolve: {
          iteminfo: function () {
            return info;
          },
          'type': function () {
            return 'qyonlyinfo';
          }
        }
      });
      modalInstance.result.then(function (showResult) {	
          $scope.pageChanged();
      });
    }
  }


  $scope.progress= function (index) {
      $scope.item = $scope.a[index];			
      var modalInstance = $modal.open({
        template: require('../views/project_progress.html'),
        controller: 'project_progresss',
        size: 'lg',
        resolve: {
          items: function () {
            return $scope.item;
          },
          type: function () {
            return $stateParams.type;
          }
        }
      });
      modalInstance.result.then(function (showResult) {	
          $scope.pageChanged();
      });
  }

  //导出excel文件-------------------start-------------
  $scope.downloadExl = function(){
      $scope.newList = [];
      $scope.b.forEach(element => {
          var dic = {
            '项目名称' : element.project_name, 
            '项目类型' : element.project_build,
            '项目级别' : element.project_level,
            '项目类别' : element.project_type,
            '所属区县' : element.company_code_name,
            '申报单位' : element. view_company_name,
            '是否需要区县审核' : element.city_project,
          };
          if (element.list.length == 0){
            dic['项目状态'] = '创建草稿, 草稿时间:' + element.apply_time;
          } else {
            var newarr = [];
            var newstr = '';
            for (var i = 0; i < element.list.length; i++){
              var aa = element.list[i].logmessage;
              if (element.list[i].logmessage != undefined){
                newarr.push(element.list[i].logmessage);
              }
              newstr = newarr.join(',');
            }
            dic['项目状态'] = newstr;
          }
          $scope.newList.push(dic);
      });
      if(!$scope.newList || $scope.newList.length == 0){
          toaster.warning({'title': '', 'body': '请查询统计数据后再导出'});
          return;
      }
      // var exportList = exportData();
      
      const wopts = { bookType: 'xlsx', bookSST: false, type: 'binary' };  //这里的数据是用来定义导出的格式类型
      const wb = { SheetNames: ['Sheet1'], Sheets: {}, Props: {} };
      wb.Sheets['Sheet1'] = XLSX.utils.json_to_sheet($scope.newList);
      var blob = new Blob([str2binary(XLSX.write(wb, wopts))], {type: 'application/octet-stream' });

      var dlink = document.getElementById('dlink');
      dlink.href = URL.createObjectURL(blob);
      dlink.click();
      setTimeout(function () {
          URL.revokeObjectURL(blob); //释放这个object URL
      }, 100);

  };
  //导出excel前的二进制转码
  function str2binary(s) {
      if (typeof ArrayBuffer !== 'undefined') {
          var buf = new ArrayBuffer(s.length);
          var view = new Uint8Array(buf);
          for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
          return buf;
      } else {
          var buf = new Array(s.length);
          for (var i = 0; i != s.length; ++i) buf[i] = s.charCodeAt(i) & 0xFF;
          return buf;
      }
  }
  
//导出excel文件------------------end---------------

  // 监听
//   $scope.$watch('productflag', function(){
//     $scope.pageChanged();
//   }, true)

}
	